package com.docMan.service.impl;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.docMan.dao.BaseDao;
import com.docMan.dao.pojo.DocBook;
import com.docMan.dao.pojo.DocBookSon;
import com.docMan.service.DocBookService;
import com.docMan.service.DocBookSonService;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.FtpUtils;
import com.docMan.utils.OpenOfficeUtil;

@Service("docBookService")
public class DocBookServiceImpl extends BaseServiceImpl<DocBook> implements DocBookService {
	private HttpSession session;
	
	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}
	private DocBookSonService docBookSonService;
	
	@Autowired
	public void setDocBookSonService(DocBookSonService docBookSonService) {
		this.docBookSonService = docBookSonService;
	}

	@Autowired
	@Override
	public void setDao(BaseDao<DocBook> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}

	@Override
	public String createBook(String bookName, int bookId,List<String> imagefilePaths,int orderId,List<String> filePaths) {
		DocBook docBook;
		if(bookId==0){
			 docBook=new DocBook();
			docBook.setBookName(bookName);
			docBook.setOrderId(orderId);
			if(imagefilePaths.size()>0)
				docBook.setPhotoName(getFileName(imagefilePaths.get(0)));
			docBook.setCreateTime(new Timestamp(System.currentTimeMillis()));
			if(filePaths.size()>0)
				docBook.setFileName(getFileName(filePaths.get(0)));
			docBook.setBookTypeId(0);
			save(docBook);
		}else {
			 docBook=getById(bookId);
			docBook.setBookName(bookName);
			docBook.setOrderId(orderId);
			if(imagefilePaths.size()>0)
				docBook.setPhotoName(getFileName(imagefilePaths.get(0)));
			if(filePaths.size()>0)
				docBook.setFileName(getFileName(filePaths.get(0)));
			saveOrUpdate(docBook);
		}
		if(imagefilePaths.size()>0){
			FileInputStream in;
			try {
				in = new FileInputStream(new File(imagefilePaths.get(0)));
				String docBookFtpPath="/book/"+docBook.getId();
				FtpUtils.uploadFile(FinalConfig.getDocFtp(), getFileName(imagefilePaths.get(0)), docBookFtpPath, in);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				putFalse();
				putMessage("上传出现错误，请删除记录后重新上传");
			}
		}
		if(filePaths.size()>0){
			FileInputStream in;
			try {
				in = new FileInputStream(new File(filePaths.get(0)));
				String docBookFtpPath="/book/"+docBook.getId();
				FtpUtils.uploadFile(FinalConfig.getDocFtp(), getFileName(filePaths.get(0)), docBookFtpPath, in);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				putFalse();
				putMessage("上传出现错误，请删除记录后重新上传");
			}
		}
		putSuccess();
		return getMap();
	}
	private String getFileName(String path) {
		String fileName = path.substring(path.lastIndexOf("\\") + 1,
				path.length());
		return fileName;
	}
	
	@Override
	@Transactional
	public String delBook(int bookId) {
		delete(bookId);
		docBookSonService.excudeSqlUpdate("delete from doc_book_son where doc_book_son.fatherBookId=?", bookId);
		putSuccess();
		return getMap();
	}

	@Override
	public String editBook(int bookId, String bookName) {
		DocBook docBook=getById(bookId);
		docBook.setBookName(bookName);
		saveOrUpdate(docBook);
		putSuccess();
		return getMap();
	}
	
	@Override
	public File getBook(int bookId) {
		File file=null;
		if(bookId!=0){
			DocBook docBook=getById(bookId);
			//0表示没有上传文件
			try {
				if(docBook.getFileName().equals("")||null==docBook.getFileName())
					return new File("");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return new File("");
			}
			String remotePath="/book/"+docBook.getId();
			String filename=docBook.getFileName();
			
//			String localPath=FinalConfig.getFtpDownloadLocalPath(session.getAttribute(FinalConfig.SESSION_HUMANID));
			Random random1=new Random(10000);
			Random random2=new Random(10000);
			String localPath=FinalConfig
					.getFtpDownloadLocalPath(random1.nextInt(10000)+random2.nextInt(10000));
			if(FtpUtils.downloadFile(FinalConfig.getDocFtp(), filename, remotePath, localPath)){
				file=new File(localPath+filename);
			}
			if(!filename.endsWith("pdf")){
				String word2pdfFilePaht=localPath+filename.replace(".docx", ".pdf").replace(".doc", ".pdf");
				try {
					OpenOfficeUtil.office2PDF(file.getCanonicalPath(), word2pdfFilePaht);
					file=new File(word2pdfFilePaht);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}
}
